package com.crm.controller;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.crm.model.mpojo.Repairrecord;
import com.crm.model.mpojo.Tasks;
import com.crm.services.RepairServices;
import com.crm.services.RepairrecordServices;
import com.crm.services.TaskServices;

@Controller
@RequestMapping("/aftersale")
public class RepairrecordController {
	@Autowired
	RepairrecordServices repairrecordServices;
	@Autowired
	TaskServices taskServices;
	@Autowired
	RepairServices repairServices;
	
	/**
	 * 根据工单编号查询单条
	 */
	@PostMapping("/repairrecordOne")
	@ResponseBody
	public JSONObject getRepairrecordByid(@RequestParam("ID") Integer repid) {
		JSONObject jsonObject = new JSONObject();
		List<Map<String, String>> selectById = repairrecordServices.selectByID(repid);
		System.out.println(selectById);
		jsonObject.put("repairrecordOne", selectById);
		return jsonObject;
	}
		
	
	/**
	 * 添加
	 * @param gdid
	 * @param statu
	 * @param date
	 * @param manid
	 * @param time
	 * @param remark
	 * @return
	 */
	@PostMapping("/addrepairrecord")
	@ResponseBody
	public String addrepair(@RequestParam(name = "id", required = false) Integer gdid,
			@RequestParam(name = "statu", required = false) Integer statu,
			@RequestParam(name = "date", required = false) String date,
			@RequestParam(name = "man", required = false) Integer manid,
			@RequestParam(name = "time", required = false) String time,
			@RequestParam(name = "name", required = false) String remark) {
		System.out.println("时间："+time+"\t日期："+date);
		Date newdate = Date.valueOf(date.substring(0, 10));
		Time newtime = Time.valueOf(time);
		//实例化对象
		Repairrecord repairrecord = new Repairrecord();
		repairrecord.setRepId(gdid);
		repairrecord.setRcId(gdid);
		repairrecord.setRcManid(manid);
		repairrecord.setRcResult(1433);
		repairrecord.setRcRemark(remark);
		repairrecord.setRcTimeok(newtime);
		repairrecord.setRcDateok(newdate);
		//添加记录
		for (int i=0;i<=0;i++) {
			if (i==0) {
				int add = repairrecordServices.add(repairrecord);
			}else {
				System.out.println("一个维修工单只能生产一条记录！");
			}
		}
		return "添加维修明细记录！";
	}

	/**
	 * 维修更新
	 * @param rcId
	 * @param statu
	 * @param starttime
	 * @param manid
	 * @param endtime
	 * @param remark
	 * @return
	 * @throws ParseException 
	 */
	@PostMapping("/updaterepairrecordOne")
	@ResponseBody
	public String updaterepair(@RequestParam(name = "id", required = false) Integer rcId,
			@RequestParam(name = "statu", required = false) Integer statu,
			@RequestParam(name = "starttime", required = false) String starttime,
			@RequestParam(name = "man", required = false) Integer manid,
			@RequestParam(name = "endtime", required = false) String endtime,
			@RequestParam(name = "remark", required = false) String remark) throws ParseException {
		//转化日期
		System.out.println("开始时间："+starttime+"结束时间："+endtime);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(manid);
		//实例化对象
		Repairrecord repairrecord = new Repairrecord();
		repairrecord.setRepId(rcId);
		repairrecord.setRcId(rcId);
		repairrecord.setPManid(manid);
		repairrecord.setPStatu(1436);
		if (starttime!=null || starttime!="") {
			repairrecord.setPStarttime(sdf.parse(starttime));
		}
		if (endtime!=null || endtime!="") {
			repairrecord.setPEndtime(sdf.parse(endtime));
		}
		//更新记录
		repairrecordServices.update(repairrecord);
		//工单完成后 根据工单id生成待办任务
		Tasks tasks = new Tasks();
		tasks.setCareId(rcId);//工单id
		//根据工单id查询对应信息
		Map<String, String> selectByidOne = repairServices.selectByidOne(rcId);
		tasks.setCustId(Integer.parseInt(String.valueOf(selectByidOne.get("custid"))));//任务客户
		tasks.setConId(Integer.parseInt(String.valueOf(selectByidOne.get("conid"))));//任务联系人
		tasks.setTaskManid(Integer.parseInt(String.valueOf(selectByidOne.get("getPerson"))));//任务执行人
		tasks.setTaskDescription("工单回访");//任务主题
		tasks.setCreatMan(Integer.parseInt(String.valueOf(selectByidOne.get("getPerson"))));//任务创建人
		//获取当前时间转换时间格式保存
		System.out.println("创建时间："+String.valueOf(selectByidOne.get("getTime")));
		tasks.setTaskTime(java.sql.Time.valueOf(String.valueOf(selectByidOne.get("getTime"))));//创建时间
		tasks.setCreatDate(java.sql.Date.valueOf(String.valueOf(selectByidOne.get("getDate"))));//任务创建时间
		tasks.setTaskDate(java.sql.Date.valueOf(String.valueOf(selectByidOne.get("payDate"))));//任务支付日期
		tasks.setDeletstatue(1206);//任务状态
		tasks.setTaskPriority(1361);//任务优先级（默认高）
		tasks.setTaskStatus(1428);//任务执行状态（未结束）
		taskServices.addTasks(tasks);//添加
		System.out.println("根据工单自动生成任务：\n"+taskServices.addTasks(tasks));
		return "更新维修明细记录1！";
	}
	
	/**交付更新
	 * @param rcId
	 * @param statu
	 * @param starttime
	 * @param manid
	 * @param endtime
	 * @param remark
	 * @return
	 */
	@PostMapping("/updaterepairrecordTwo")
	@ResponseBody
	public String updaterepairt(@RequestParam(name = "id", required = false) Integer rcId,
			@RequestParam(name = "statu", required = false) Integer statu,
			@RequestParam(name = "date", required = false) String date,
			@RequestParam(name = "man", required = false) Integer manid,
			@RequestParam(name = "time", required = false) String time,
			@RequestParam(name = "remark", required = false) String remark) {
		Date newdate = Date.valueOf(date.substring(0, 10));
		Time newtime = Time.valueOf(time);
		//实例化对象
		Repairrecord repairrecord = new Repairrecord();
		repairrecord.setRepId(rcId);
		repairrecord.setRcId(rcId);
		repairrecord.setGStatu(statu);
		repairrecord.setGDate(newdate);
		repairrecord.setGTime(newtime);
		repairrecord.setGCust(manid);
		repairrecord.setGRemark(remark);
		//更新记录
		repairrecordServices.update(repairrecord);
		return "更新维修明细记录2！";
	}
	
}
